import {showNotify, showSuccessToast, showConfirmDialog, showToast} from 'vant'
import api from '@/http/api'
import router from '@/router'


const UPLOAD_FILE_SUCCESS = 'UPLOAD_FILE_SUCCESS'
const DELETE_FILE_SUCCESS = 'DELETE_FILE_SUCCESS'
const SET_OPENID = 'SET_OPENID'
const SET_VOL_ID = 'SET_VOL_ID'
const SET_APPROVE_STATUS = 'SET_APPROVE_STATUS'
const SET_INFO = 'SET_INFO'
const SET_BANK_CARD_NO = 'SET_BANK_CARD_NO'
const SET_ID_CARD_NO = 'SET_ID_CARD_NO'
const SET_DOCTOR_ID = 'SET_DOCTOR_ID'
const SET_QUESTION_STATUS = 'SET_QUESTION_STATUS'

export default {
    namespaced: true,
    state: {
        info: {
            id: '',
            name: '',
            openid: '',
            phone: '',
            email: '',
            cardNumber: '',
            hospitalId: '',
            hospitalName: '',
            departmentName: '',
            titleName: '',
            doctorCertificateCode: '',
            bankName: '',
            subBranch: '',
            bankNumber: '',
            volunteerId: '',
            licensedPhysician: [],
			informedConsent: [],
            idCardFront: [],
            idCardRevers: [],
            bankPicture: [],
            approveStatus: '',
            questionStatus: 0,
            area: '',
            areaId: '',
            city: '',
            cityId: '',
            province: '',
            provinceId: ''
        },
        loginForm: {
            phone: '',
            passwd: '',
            openid: ''
        }

    },

    getters: {
		doctorInfo: state => {
			return state.info
		}
	},

    mutations: {
        [UPLOAD_FILE_SUCCESS]: (state, rowFile) => {
            state.info[rowFile.type].push(rowFile.url)
        },
        [DELETE_FILE_SUCCESS]: (state, rowFile) => {
            console.log(state.info)
            state.info[rowFile.type].forEach((item, index) => {
                if(rowFile.url.indexOf(item) >= 0) {
                    state.info[rowFile.type].splice(index, 1)
                }
            })
        },
        [SET_OPENID]: (state, openid) => {
            state.info.openid = openid
        },
        [SET_DOCTOR_ID]: (state, id) => {
            state.info.id = id
        },
        [SET_VOL_ID]: (state, volId) => {
            state.info.volunteerId = volId
        },
        [SET_APPROVE_STATUS]: (state, status) => {
            state.info.approveStatus = status
        },
		[SET_INFO] (state, val = {}) {
			state.info = {
				...state.info,
				...val
			}
			// sessionStorage.setItem('doctorInfo', JSON.stringify(state.info))
		},
        [SET_ID_CARD_NO]: (state, cardNo) => {
            state.info.cardNumber = cardNo
        },
        [SET_BANK_CARD_NO]: (state, bankCardNo) => {
            state.info.bankNumber = bankCardNo
        },
        [SET_QUESTION_STATUS]: (state, questionStatus) => {
            state.info.questionStatus = questionStatus
        }

    },

    actions: {
        submit({ commit, state }, { isUpdate }) {
            console.log(isUpdate)
            if(!state.info.idCardFront) {
                showNotify({ type: 'danger', message: '请上传身份证正面照片' })
                return false
            }
            if(!state.info.idCardRevers) {
                showNotify({ type: 'danger', message: '请上传身份证反面照片' })
                return false
            }
            if(state.info.licensedPhysician.length == 0) {
                showNotify({ type: 'danger', message: '请上传医师资格证照片' })
                return false
            }
            if(!state.info.bankPicture) {
                showNotify({ type: 'danger', message: '请上传银行卡照片' })
                return false
            }
            let openid = sessionStorage.getItem('openid')
            commit(SET_OPENID, openid)
            let volId = state.info.volunteerId || sessionStorage.getItem('masterid')
            commit(SET_VOL_ID, volId)
            showConfirmDialog({
                title: '提示',
                message: '确认提交您的注册信息么？',
            }).then(() => {
                api[isUpdate ? 'updateDoctor' : 'register'](state.info).then(res => {
                    if(res.code === 0) {
                        showSuccessToast('提交成功')
                        commit(SET_APPROVE_STATUS, 1)
                        if(!isUpdate) {
                            commit(SET_DOCTOR_ID, res.data.id)
                        }
                        router.replace({path: '/doctornotice'})
                    }
                })
            }).catch(() => {
            })

        },

        async loginByOpenid({commit, state}, cb) {},
        
    },
}
